import _COIGIG from '../runtime/mapdata'
import DataBus  from '../databus'
/**
 * 游戏基础的精灵类
 */


 	
	
 
let databus6 = new DataBus()


export default class Sprite {
  constructor(imgSrc = '', width=  0, height = 0, x = 0, y = 0) {
    this.img     = new Image()
    this.img.src = imgSrc

    //this.MAP_OFFSETX=databus6.MAP_OFFSETX //x offset
    //this.MAP_OFFSETY=databus6.MAP_OFFSETY //y offset
  
	

	this.width  = width
    this.height = height

    this.x = x
    this.y = y
	
	var index=databus6.GAME_INDEX
    this.x_length=_COIGIG[index]['map'][0].length
	this.y_length=_COIGIG[index]['map'].length
    
	//define two dimension map data
	if (databus6.mapdataflag==false){
		databus6.mapdata = new Array();	
		for (var j = 0; j<this.y_length;j++){ //this.databus6.mapdata定义在sprite类
		   databus6.mapdata[j]=new Array();
		}
		
		//取地图数据
	    this.getmapdata(index)
	    databus6.mapdataflag=true
	}
	//define end
	
	
	
	
    this.visible = true
  }
  
    getmapdata(mapindex){
	   var mapi=_COIGIG[mapindex]['map'];
	   //console.log(mapi[0][0]);
	   
	   for (var j = 0; j<this.y_length;j++)
		   for (var i=0;i<this.x_length;i++){
		       databus6.mapdata[j][i]=mapi[j][i]
			   //console.log(this.databus6.mapdata[j][i])
		   }
	}

  //地图坐标转画布坐标
    coord2position (cx,cy){
		return {
            x:databus6.MAP_OFFSETX+cx*databus6.size+databus6.size/2,
            y:databus6.MAP_OFFSETY+cy*databus6.size+databus6.size/2
        };
    }
    
	//画布坐标转地图坐标
    position2coord (x,y){
		//this.x,this.y=0 :地图的左上起点坐标 (用不着，删除了)
        
		
		return {
            x:Math.floor((x-databus6.MAP_OFFSETX)/databus6.size),
            y:Math.floor((y-databus6.MAP_OFFSETY)/databus6.size)
            
        };
    }
	
	//获取地图上某点的值
    get(x,y){
        if(databus6.mapdata[y]&&typeof databus6.mapdata[y][x]!='undefined'){
            return databus6.mapdata[y][x];
        }
		//console.log("err")
        return -1;
    }
    
	//设置地图上某点的值
    set(x,y,value){
        if(databus6.mapdata[y]){
            databus6.mapdata[y][x] = value;
        }
    }
	
	setbeans(beans){
	  this.beans =beans
    }

	
	
	//寻址算法
    finder (params){
        var defaults = {
            map:null,
            start:{},
            end:{},
			type:'path'
        };
        var options = Object.assign({},defaults,params);
        if(options.map[options.start.y][options.start.x]||options.map[options.end.y][options.end.x]){ //当起点或终点设置在墙上
            return [];
        }
        var finded = false;
        var result = [];
        var y_length  = options.map.length;
        var x_length = options.map[0].length;
        var steps = [];     //步骤的映射
        for(var y=y_length;y--;){
            steps[y] = new Array(x_length).fill(0);
        }
        
		var _getValue = function(from_x,from_y,to_x,to_y){  //获取地图上的目标值 
           //if (databus6.mapmoving==false){
			  
             if (options.map[to_y]&&typeof options.map[to_y][to_x]!='undefined'){
                return options.map[to_y][to_x];
              
			 }
             return -1;
		
        };
		
		
        var _next = function(from,to){ //判定是否可走,可走放入列表
            var value = _getValue(from.x,from.y,to.x,to.y);
            if(value<1){
                if(value==-1){ //走进两边的出口，会从另一边出现
                    to.x = (to.x+x_length)%x_length;
                    to.y = (to.y+y_length)%y_length;
                    to.change = 1;
                }
                if(!steps[to.y][to.x]){
                    result.push(to);
                }
            }
        };
        var _render = function(list){//找线路
            var new_list = [];
            var next = function(from,to){
                var value = _getValue(from.x,from.y,to.x,to.y);
                if(value<1){	//当前点是否可以走
                    if(value==-1){
                        to.x = (to.x+x_length)%x_length;
                        to.y = (to.y+y_length)%y_length;
                        to.change = 1;
                    }
                    if(to.x==options.end.x&&to.y==options.end.y){ //找到目标位置
                        steps[to.y][to.x] = from;
                        finded = true;
                    }else if(!steps[to.y][to.x]){
                        steps[to.y][to.x] = from;
                        new_list.push(to);
                    }
                }
            };
			
            list.forEach(function(current){
				next(current,{y:current.y+1,x:current.x}); //from ,to 
                next(current,{y:current.y,x:current.x+1}); //from ,to 
                next(current,{y:current.y-1,x:current.x}); //from ,to 
                next(current,{y:current.y,x:current.x-1}); //from ,to 
            });
            if(!finded&&new_list.length){ //finded :end tag
                _render(new_list);
            }
        };
        _render([options.start]); //start to find a way
        
		if(finded){
            var current=options.end;
            if(options.type=='path'){
                while(current.x!=options.start.x||current.y!=options.start.y){ //倒退回到起点，保存当中的路径
                    result.unshift(current);
                    current=steps[current.y][current.x];
                }
            }else if(options.type=='next'){
                _next(current,{x:current.x+1,y:current.y});
                _next(current,{x:current.x,y:current.y+1});
                _next(current,{x:current.x-1,y:current.y});
                _next(current,{x:current.x,y:current.y-1});
            }
        }
        return result;
    }
  
  
}
